Xbasic

json_path Function

Syntax

c string = json_path(c json, c path)

Arguments

jsonCharacter

A JSON string

pathCharacter

The path defining what portion of the JSON to extract.

Returns

stringCharacter

Returns the portion of the JSON that matches the path.

Description

Extracts a portion of a JSON string.

Discussion

The json_path() function allows you to extract portions of a JSON string and perform certain types of simple calculations on a JSON string.

When working with XML data, there is a well known standard, called XPath, for extracting parts of the XML data. JSON Path allows for similar functionality when working with JSON data.

For example:

dim path as c

'get the author for all books in the store
path = "$.store.book[*].author"

dim json as c

json = <<%json%
{
  "store": {
    "book": [
      {
        "category": "reference",
        "author": "Nigel Rees",
        "title": "Sayings of the Century",
        "price": 8.95
      },
      {
        "category": "fiction",
        "author": "Evelyn Waugh",
        "title": "Sword of Honour",
        "price": 12.99
      },
      {
        "category": "fiction",
        "author": "Herman Melville",
        "title": "Moby Dick",
        "isbn": "0-553-21311-3",
        "price": 8.99
      },
      {
        "category": "fiction",
        "author": "J. R. R. Tolkien",
        "title": "The Lord of the Rings",
        "isbn": "0-395-19395-8",
        "price": 22.99
      }
    ],
    "bicycle": {
      "color": "red",
      "price": 19.95
    }
  }
}
%json%

dim result as c

result = json_path(json,path)

= [
    "Nigel Rees",
    "Evelyn Waugh",
    "Herman Melville",
    "J. R. R. Tolkien"
]

The chart below describes what will be returned by the json_path() function for the specified path. The examples in this chart are from the NPM JSONPath documentation:

JSON Path Syntax

Returns

$.store.book[*].author

The authors of all books in the store

$..author

All authors

$.store.*

All things in store, which are its books (a book array) and a red bicycle (a bicycle object).

$.store..price

The price of everything in the store.

$..book[2]

The third book (book object)

$..book[(@.length-1)]

The last book in order.

$..book[-1:]

$..book[0,1]

The first two books

$..book[:2]

$..book[0][category,author]

The categories and authors of all books

$..book[?(@.isbn)]

Filter all books with an ISBN number

$..book[?(@.price<10)]

Filter all books cheaper than 10

$..*[?(@property === 'price' && @ !== 8.95)]

Obtain all property values of objects whose property is price and which does not equal 8.95

$

The root of the JSON object (i.e., the whole object itself)

$..*

All Elements (and text) beneath root in an XML document. All members of a JSON structure beneath the root.

$..

All Elements in an XML document. All parent components of a JSON structure including root.

$..[?(@.price>19)]^

Parent of those specific items with a price greater than 19 (i.e., the store value as the parent of the bicycle and the book array as parent of an individual book)

$.store.*~

The property names of the store sub-object ("book" and "bicycle"). Useful with wildcard properties.

$.store.book[?(@path !== "$['store']['book'][0]")]

All books besides that at the path pointing to the first

$..book[?(@parent.bicycle && @parent.bicycle.color === "red")].category

Grabs all categories of books where the parent object of the book has a bicycle child whose color is red (i.e., all the books)

$..book.*[?(@property !== "category")]

Grabs all children of "book" except for "category" ones

$..book[?(@property !== 0)]

Grabs all books whose property (which, being that we are reaching inside an array, is the numeric index) is not 0

$.store.*[?(@parentProperty !== "book")]

Grabs the grandchildren of store whose parent property is not book (i.e., bicycle's children, "color" and "price")

$..book.*[?(@parentProperty !== 0)]

Get the property values of all book instances whereby the parent property of these values (i.e., the array index holding the book item parent object) is not 0

$..book..*@number()

Get the numeric values within the book array

json_path in Javascript

You can also use JSON Path on the client side (in your Javascript code). The Javascript function is also called json_path().

See Also